<template>
    <div class="profileBox">
        <div class="left">
            <div class="card">
                <div class="title">
                    profile
                </div>
                <div class="image">
                    <img :src="modelData.model.headImage" alt="">
                </div>
                <div class="card-info">
                    <span class="name">George Johnson</span>
                    <p>SEX&nbsp;&nbsp;:&nbsp;&nbsp;<span>{{ modelData.model.sex === 1 ? 'Male' : 'Female' }}</span></p>
                    <p>HEIGHT&nbsp;&nbsp;:&nbsp;&nbsp;<span>{{ modelData.model.height }}</span></p>
                    <p>BWH&nbsp;&nbsp;:&nbsp;&nbsp;<span>{{ modelData.model.bwh }}</span></p>
                    <p>HAIR&nbsp;&nbsp;:&nbsp;&nbsp;<span>{{ modelData.model.hair }}</span></p>
                    <p>EYES&nbsp;&nbsp;:&nbsp;&nbsp;<span>{{ modelData.model.eyes }}</span></p>
                    <p>SHOES&nbsp;&nbsp;:&nbsp;&nbsp;<span>{{ modelData.model.shoes }}</span></p>
                </div>
                <div>
                    <button type="button" class="button" @click="dialogVisible = true">
                        <span class="button__text">Add an image</span>
                        <span class="button__icon"><svg xmlns="http://www.w3.org/2000/svg" width="24"
                                viewBox="0 0 24 24" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"
                                stroke="currentColor" height="24" fill="none" class="svg">
                                <line y2="19" y1="5" x2="12" x1="12"></line>
                                <line y2="12" y1="12" x2="19" x1="5"></line>
                            </svg></span>
                    </button>
                </div>
            </div>
        </div>
        <div class="right">
            <!-- 放图片的地方 -->

            <div class="pictureBox" v-for="(item, index) in modelData.images">
                <div class="image">
                    <img :src="item.imageUrl" alt="">
                    <div class="zhezhao">
                        <el-icon @click="deleteImage(item.id)" style="color:white;font-weight: bold;font-size: 25px;">
                            <Delete />
                        </el-icon>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <el-dialog v-model="dialogVisible" title="上传" width="700">
        <el-upload class="upload-demo" drag :action="baseUrl+'/model/admin/upload'" :headers="headers"
            name="file" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <el-icon class="el-icon--upload"><upload-filled /></el-icon>
            <div class="el-upload__text">
                Drop file here or <em>click to upload</em>
            </div>
            <template #tip>
                <div class="el-upload__tip">
                    <!-- 只允许jpg/png图片 -->
                    jpg/png files with a size less than 500kb
                </div>
            </template>
        </el-upload>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">Cancel</el-button>
                <el-button type="primary" @click="dialogVisible = false">
                    Confirm
                </el-button>
            </div>
        </template>
    </el-dialog>

</template>

<script setup lang="ts">
import { addModelImagesAPI, deleteImageAPI } from '@/apis/admin';
import { searchModelInfoAPI } from '@/apis/user';
import { ElMessage } from 'element-plus';
import { onMounted } from 'vue';
import { ref } from 'vue'
import { useRoute } from 'vue-router';
import type { UploadProps } from 'element-plus'
import { useUserStore } from '@/stores/userStore';

import { baseUrl } from '@/utils/baseUrl';

const dialogVisible = ref(false)

const deleteImage = async (id:number) => {

    let flag = confirm('Whether you want to delete the current picture')

    if (flag === false) return

    const res = await deleteImageAPI(id)

    if (res.data.code === 200) {
        ElMessage.success('The deletion is successful')

        getInfo()
    }
    else {
        ElMessage.error(res.data.msg)
    }
}

const modelData = ref({
    "model": {
        "id": 1,
        "name": "YAROSLAV",
        "headImage": "无",
        "sex": 1,
        "height": "180CM",
        "bwh": "90-66-92",
        "hair": "Black",
        "eyes": "Brown",
        "shoes": "42"
    },
    "images": [
        {
            "id": 1,
            "modelId": 1,
            "imageUrl": "1----"
        }
    ]
})

const route = useRoute()

const getInfo = async () => {

    const res = await searchModelInfoAPI(parseInt(route.params.id as string))

    if (res.data.code === 200) {
        modelData.value = res.data.data
        console.log(modelData.value)
    }
    else {
        ElMessage.error(res.data.msg)
    }


}

onMounted(() => {
    getInfo()
})

const headers = {
    token: useUserStore().getUserInfo().token
}

const handleAvatarSuccess: UploadProps['onSuccess'] = async (
    response
) => {
    // ElMessage.success('上传成功')

    const res = await addModelImagesAPI(parseInt(route.params.id as string), response.data)
    if (res.data.code === 200) {
        ElMessage.success('The upload is successful')
        dialogVisible.value = false
        getInfo()
    }
    else {
        ElMessage.error(res.data.msg)
    }

}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}

</script>

<style lang="scss" scoped>
.profileBox {
    display: flex;
    flex-wrap: wrap;

    .right {
        flex: 1;
        min-width: 260px;
        margin-right: 20px;
        border: 1px dashed #666;
        padding: 20px;
        box-sizing: border-box;
        max-height: calc(100vh - 150px);
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(240px, 300px));
        // align-items: start;
        gap: 20px;
        overflow-y: scroll;
        // overflow-x: hidden;

        .pictureBox {

            display: flex;
            flex-direction: column;
            overflow: hidden;
            height: 300px;
            width: 300px;
            // margin-bottom: 20px;

            .image {
                overflow: hidden;
                position: relative;

                .zhezhao {
                    width: 100%;
                    height: 100%;
                    position: absolute;
                    top: 0;
                    background: rgba(0, 0, 0, 0.44);
                    opacity: 0;
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                    align-items: center;
                    transition: all .4s;
                }

                &:hover {

                    cursor: pointer;

                    img {
                        transform: scale(1.2);
                    }

                    .zhezhao {
                        opacity: 1;
                    }
                }
            }



            img {
                width: 100%;
                transition: all .4s;
                overflow: hidden;
            }

            .name {
                background-color: #f1f1f1;
                height: 40px;
                line-height: 40px;
                padding: 0 15px;
                color: #333333;
            }
        }
    }

    .left {
        margin-right: 20px;
        margin-bottom: 20px;
        flex: 1;
        min-width: 300px;
        max-width: 500px;

        .title {
            font-size: 18px;
            border: 1px solid #666;
            padding: 5px 20px;
            font-style: italic;
            color: white;
            background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
            cursor: pointer;
        }



        .card {
            // background-color: aqua;
            border: 1px dashed #666;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            box-sizing: border-box;
            padding: 20px;
            // justify-content: center;

            .image {
                position: relative;
                display: flex;
                justify-content: center;
            }

            img {
                width: 100px;
                height: 100px;
                border-radius: 50%;
                margin-top: 20px;
            }

            .card-info {

                margin-top: 40px;

                .name {
                    font-size: 20px;
                    display: flex;
                    justify-content: center;
                    margin-top: 10px;
                    margin-bottom: 20px;
                }

                p {
                    display: flex;
                    justify-content: center;
                    color: black;
                    line-height: 50px;
                    // font-size: 20px;

                    span {
                        font-size: 16px;
                        color: #666;
                    }
                }
            }
        }
    }
}

// button
/* From Uiverse.io by cssbuttons-io */
.fancy {
    background-color: transparent;
    border: 2px solid #000;
    border-radius: 0;
    box-sizing: border-box;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    // float: right;
    font-weight: 700;
    letter-spacing: 0.05em;
    margin: 0;
    outline: none;
    overflow: visible;
    padding: 1.25em 2em;
    position: relative;
    text-align: center;
    text-decoration: none;
    text-transform: none;
    transition: all 0.3s ease-in-out;
    user-select: none;
    font-size: 13px;
}

.fancy::before {
    content: " ";
    width: 1.5625rem;
    height: 2px;
    background: black;
    top: 50%;
    left: 1.5em;
    position: absolute;
    transform: translateY(-50%);
    transform-origin: center;
    transition: background 0.3s linear, width 0.3s linear;
}

.fancy .text {
    font-size: 1.125em;
    line-height: 1.33333em;
    padding-left: 2em;
    display: block;
    text-align: left;
    transition: all 0.3s ease-in-out;
    text-transform: uppercase;
    text-decoration: none;
    color: black;
}

.fancy .top-key {
    height: 2px;
    width: 1.5625rem;
    top: -2px;
    left: 0.625rem;
    position: absolute;
    background: #e8e8e8;
    transition: width 0.5s ease-out, left 0.3s ease-out;
}

.fancy .bottom-key-1 {
    height: 2px;
    width: 1.5625rem;
    right: 1.875rem;
    bottom: -2px;
    position: absolute;
    background: #e8e8e8;
    transition: width 0.5s ease-out, right 0.3s ease-out;
}

.fancy .bottom-key-2 {
    height: 2px;
    width: 0.625rem;
    right: 0.625rem;
    bottom: -2px;
    position: absolute;
    background: #e8e8e8;
    transition: width 0.5s ease-out, right 0.3s ease-out;
}

.fancy:hover {
    color: white;
    background: black;
}

.fancy:hover::before {
    width: 0.9375rem;
    background: white;
}

.fancy:hover .text {
    color: white;
    padding-left: 1.5em;
}

.fancy:hover .top-key {
    left: -2px;
    width: 0px;
}

.fancy:hover .bottom-key-1,
.fancy:hover .bottom-key-2 {
    right: 0;
    width: 0;
}

// 添加
/* From Uiverse.io by andrew-demchenk0 */
.button {
    position: relative;
    width: 180px;
    height: 40px;
    cursor: pointer;
    display: flex;
    align-items: center;
    // border: 1px solid #34974d;
    border: 0;
    // background-color: #3aa856;
    background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
}

.button,
.button__icon,
.button__text {
    transition: all 0.3s;
}

.button .button__text {
    transform: translateX(30px);
    color: #fff;
    font-weight: 600;
}

.button .button__icon {
    position: absolute;
    transform: translateX(139px);
    height: 100%;
    width: 39px;
    // background-color: #34974d;
    background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
    display: flex;
    align-items: center;
    justify-content: center;
}

.button .svg {
    width: 30px;
    stroke: #fff;
}

.button:hover {
    // background: #34974d;
    background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
}

.button:hover .button__text {
    color: transparent;
}

.button:hover .button__icon {
    width: 178px;
    transform: translateX(0);
}

.button:active .button__icon {
    // background-color: #2e8644;
    background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
}

.button:active {
    // border: 1px solid #2e8644;
    background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
}
</style>